<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8">
		<title>导入人天签到数据</title>
		<script src="jquery.js"></script>
		<script src="xlsx.core.min.js"></script>
	</head>
	<body>
		<table border="1" width="50%" cellpadding="0" cellspacing="0">
			<tr>
				<td colspan="2"><h2>导入人天签到数据</h2></td>
			</tr>
			<tr height="30">
				<td align="right">文件：</td>
				<td align="left"><input type="file" id="excel-file"></td>
			</tr>
			<tr height="30">
				<td align="right">月份：</td>
				<td align="left"><input type="month" id="month" /></td>
			</tr>
			<tr height="30" align="centers">
				<td colspan="2"><button onclick="submitService()">提交</button></td>
			</tr>
		</table>
		<script>
			var list;
			//给input标签绑定change事件，一上传选中的.xls文件就会触发该函数
			$('#excel-file').change(function(e) {
				var files = e.target.files;
				var fileReader = new FileReader();
				fileReader.onload = function(ev) {
					try {
						var data = ev.target.result
						var workbook = XLSX.read(data, {
							type: 'binary'
						}) // 以二进制流方式读取得到整份excel表格对象
						var persons = []; // 存储获取到的数据
					} catch (e) {
						console.log('文件类型不正确');
						return;
					}
					// 表格的表格范围，可用于判断表头是否数量是否正确
					var fromTo = '';
					// 遍历每张表读取
					for (var sheet in workbook.Sheets) {
						if (workbook.Sheets.hasOwnProperty(sheet)) {
							fromTo = workbook.Sheets[sheet]['!ref'];
							console.log(fromTo);
							persons = persons.concat(XLSX.utils.sheet_to_json(workbook.Sheets[sheet]));
							//break; // 如果只取第一张表，就取消注释这行
						}
					}
					//在控制台打印出来表格中的数据
					//console.log(persons);
					list = persons;
				};
				// 以二进制方式打开文件
				fileReader.readAsBinaryString(files[0]);
			});
			let forms = [];

			function getUser(name) {
				let user;
				for (var i = 0; i < forms.length; i++) {
					let form = forms[i];
					if (form && form.name == name) {
						return form;
					}
				}
				if (!user) {
					user = {
						"name": name,
						"signDays": 0.0
					};
					forms.push(user);
				}
				return user;
			}

			function submitService() {
				if (list) {
					var signs = [];
					for (var i = 0; i < list.length; i++) {
						let row = list[i];
						signs.push({name:row.人员名称,signDays:row.项目签到人天,projectCode:row.项目编码})
					}
					
					//ajax提交
					let month = document.getElementById("month").value;
					if (!month) {
						alert("请选择月份")
						return;
					}
					month = month.replace("-", "");
					let body = {
						"list": signs,
						"month": month
					}

					$.ajax({
						// url
						url: 'http://' + location.hostname + ':8080/ProjectWorkerSign/add/list',
						// 参数
						data: JSON.stringify(body),
						// 请求类型
						type: 'post',
						// 响应体结果
						dataType: 'json',
						// 成功的回调
						success: function(data) {
							if (data.code == 2000) {
								alert("提交成功");
							} else {
								alert(data.message);
							}

						},
						// 超时时间
						timeout: 2000,
						// 失败的回调
						error: function() {
							console.log('出错了');
						},
						// 头信息
						headers: {
							"Content-Type": "application/json"
						}
					})

				} else {
					alert("请选择文件")
				}

			}
		</script>
	</body>
</html>
